package com.md.api.controller;

import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundQueryRequest;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryResult;
import com.github.binarywang.wxpay.service.WxPayService;
import com.md.api.annotation.NoRepeatSubmit;
import com.md.api.common.utils.IpUtil;
import com.md.api.common.utils.StringUtils;
import com.md.api.entity.HttpResult;
import com.md.api.entity.MotOrder;
import com.md.api.entity.MotReservation;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.entity.enums.OrderType;
import com.md.api.service.MotService;
import com.md.api.service.OrderService;
import com.md.api.service.RechargeService;
import com.md.api.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RequestMapping("/mot")
@RestController
public class MotController {
	@Autowired
	private MotService motService;
	@Autowired
	private WxPayService wxService;
	@Resource
    OrderService orderSevice;
	@Autowired
	ShoppingCartService shoppingCartService;
	@Autowired
	RechargeService rechargeService;


	//该房间所选日期的预定情况，预定数=总数时，前端需要置灰不可点
	@RequestMapping("/dayHasMot")
	@ResponseBody
	public HttpResult dayHasRoom() {
		return motService.dayHasRoom();
	}

	//该房间所选日期的预定情况，预定数=总数时，前端需要置灰不可点
	@RequestMapping("/predetermineDetail")
	@ResponseBody
	public HttpResult predetermineDetail(@RequestBody ComposeQueryDto queryDto) {
		return motService.predetermineDetail(queryDto);
	}


	@RequestMapping("/trade")
	@ResponseBody
	public HttpResult trade(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (StringUtils.isEmpty(qto.getServiceId())||StringUtils.isEmpty(qto.getSiteId())
				||StringUtils.isEmpty(qto.getPredetermineId())) {
			return HttpResult.PARAM_ERROR;
		}
		if (null!=qto.getMotType()&&qto.getMotType()==2
				&&(null==qto.getLatitude()||null==qto.getLongitude())){
			return HttpResult.fail("上门接车定位信息不能为空！");
		}
		return motService.motTrade(request,qto);
	}


	//下单
	@NoRepeatSubmit
	@RequestMapping("/order")
	@ResponseBody
	public HttpResult order(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		if (StringUtils.isEmpty(qto.getServiceId())||StringUtils.isEmpty(qto.getSiteId())
				||StringUtils.isEmpty(qto.getPredetermineId())) {
			return HttpResult.PARAM_ERROR;
		}
		if (StringUtils.isEmpty(qto.getContactPhone())) {
			return HttpResult.fail("请填写联系电话！");
		}
		try {
			return motService.order(request, qto);
		}catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("下单失败："+e.getMessage());
		}
	}

	//取消车检订单
	@NoRepeatSubmit
	@RequestMapping("/cancelMotOrder")
	@ResponseBody
	public HttpResult cancelMotOrder(HttpServletRequest request,@RequestBody MotOrder order) {
		try {
			if (StringUtils.isEmpty(order.getMotNo())) {
				return HttpResult.PARAM_ERROR;
			}
			return motService.cancelMotOrder(request, order);
		}catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("预约失败："+e.getMessage());
		}
	}

	@RequestMapping("/notify")
	public String notify(HttpServletRequest request, HttpServletResponse response) throws IOException {

		String ip = IpUtil.getRealIp(request);
		log.info("回调请求方ip："+ip);
		log.info("notify回调啦："+request.toString());
		return motService.notify(request);
	}

	@RequestMapping("/myMotOrder")
	@ResponseBody
	public HttpResult myMotOrder(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {

		return motService.myMotOrder(request,qto);
	}

	@RequestMapping("/orderDetail")
	@ResponseBody
	public HttpResult orderDetail(HttpServletRequest request,@RequestBody MotOrder order) {

		return motService.orderDetail(request,order);
	}

	//预约
	@NoRepeatSubmit
	@RequestMapping("/reservation")
	@ResponseBody
	public HttpResult reservation(HttpServletRequest request,@RequestBody ComposeCommitDto qto) {
		try {
			return motService.reservation(request, qto);
		}catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("预约失败："+e.getMessage());
		}
	}

	//预约详情
	@NoRepeatSubmit
	@RequestMapping("/reservationDetail")
	@ResponseBody
	public HttpResult reservationDetail(HttpServletRequest request,@RequestBody MotReservation reservation) {
		try {
			return motService.reservationDetail(request, reservation);
		}catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("预约失败："+e.getMessage());
		}
	}

	//取消预约
	@NoRepeatSubmit
	@RequestMapping("/cancelReservation")
	@ResponseBody
	public HttpResult cancelReservation(HttpServletRequest request,@RequestBody MotReservation reservation) {
		try {
			if (StringUtils.isEmpty(reservation.getReservationNo())) {
				return HttpResult.PARAM_ERROR;
			}
			return motService.cancelReservation(request, reservation);
		}catch (Exception e) {
			e.printStackTrace();
			return HttpResult.fail("预约失败："+e.getMessage());
		}
	}

	@RequestMapping("/backMotNotify")
	public String backMotNotify(HttpServletRequest request, HttpServletResponse response) throws IOException {

		String ip = IpUtil.getRealIp(request);
		log.info("回调请求方ip："+ip);
		log.info("backMotNotify回调啦："+request.toString());
		return motService.backMotNotify(request);
	}



	@RequestMapping("/myReservation")
	@ResponseBody
	public HttpResult myReservation(HttpServletRequest request,@RequestBody ComposeQueryDto qto) {

		return motService.myReservation(request,qto);
	}



}
